package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.enterprise.act.GiftCodeActivity;
import com.csweimei.rentmanagementtreasure.http.entity.CodeListBean;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class CodeListItemVM extends MultiItemViewModel {
    public ObservableField<CodeListBean.DataDTO> entity = new ObservableField<>();
    public ObservableField<Integer> showAll = new ObservableField<>(View.VISIBLE);
    public ObservableField<Integer> showSx = new ObservableField<>(View.GONE);

    CodeListVM codeListVM;
    public CodeListItemVM(@NonNull BaseViewModel viewModel) {
        super(viewModel);
        codeListVM = (CodeListVM) viewModel;
    }



    public void setData(CodeListBean.DataDTO bean) {
        entity.set(bean);
        if (codeListVM.state.get().equals("0")){
            showAll.set(View.VISIBLE);
            showSx.set(View.GONE);
        }else {
            showAll.set(View.GONE);
            showSx.set(View.VISIBLE);
        }
    }

    public BindingCommand copy = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) codeListVM.mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label",entity.get().getCodeX());
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtils.showShort("复制成功");
        }
    });

    public BindingCommand del = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            codeListVM.delCode(entity.get().getCodeID()+"");
        }
    });

    public BindingCommand share = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConfig.BUNDLE_KEY,entity.get());
        viewModel.startActivity(GiftCodeActivity.class,bundle);
    });




}
